package com.paw.portal.controller;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.paw.common.constant.RedisPrefixKeyConstant;
import com.paw.common.context.UserContext;
import com.paw.common.context.UserContextHolder;
import com.paw.core.util.RedisUtilService;
import com.paw.core.util.StringRedisUtilService;
import com.paw.portal.request.dto.LoginRequestDto;
import com.paw.portal.response.dto.CurrentUserDto;
import com.paw.portal.response.dto.LoginResponseDto;
import com.paw.portal.service.IAntdUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Rubble
 * @date 2021/8/22
 **/
@Slf4j
@Api(tags = "登录控制器")
@RestController
public class LoginController {

  public static final long TOKEN_EXPIRED_TIME = 3*24*60*60*1000;

  @Autowired
  private IAntdUserService antdUserService;

  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @ApiOperation("登录")
  @PostMapping("/api/portal/login/account")
  public LoginResponseDto login(LoginRequestDto requestDto){
    log.info("login {}", JSONUtil.toJsonStr(requestDto));
    String json = "{ \"status\": \"ok\", \"type\": 13, \"currentAuthority\": \"user\" }";
    LoginResponseDto loginResponseDto = JSONUtil.toBean(json, LoginResponseDto.class);

    UserContext userContext = new UserContext();
    userContext.setId(1L);
    userContext.setLoginName("admin");
    userContext.setName("系统管理员");
    Set<String> permissions = new HashSet<>();
    permissions.add("admin");
    userContext.setPermissions(permissions);

    // 生成 token
    String token = UUID.fastUUID().toString(true);

    String tokenKey = RedisPrefixKeyConstant.getKey(RedisPrefixKeyConstant.PRE_TOKEN,token);
    // 放入redis后由 paw-gateway globalFilter 验证解析token  由 UserInfoInterceptor 放入线程
    String userInfoStr = JSON.toJSONString(userContext);
    stringRedisTemplate.opsForValue().set(tokenKey,userInfoStr,TOKEN_EXPIRED_TIME, TimeUnit.MILLISECONDS);

    loginResponseDto.setToken(token);

    return loginResponseDto;
  }

  @ApiOperation("获取当前用户信息")
  @GetMapping("/api/portal/currentUser")
  public CurrentUserDto getCurrentUser(){

    UserContext userContext = UserContextHolder.getUser();
    log.info("useContext {}", JSON.toJSONString(userContext));

    String json = "{\"name\":\"Serati Ma-8080\",\"avatar\":\"https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png\",\"userid\":\"00000001\",\"email\":\"antdesign@alipay.com\",\"signature\":\"海纳百川，有容乃大\",\"title\":\"交互专家\",\"group\":\"蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED\",\"tags\":[{\"key\":\"0\",\"label\":\"很有想法的\"},{\"key\":\"1\",\"label\":\"专注设计\"},{\"key\":\"2\",\"label\":\"辣~\"},{\"key\":\"3\",\"label\":\"大长腿\"},{\"key\":\"4\",\"label\":\"川妹子\"},{\"key\":\"5\",\"label\":\"海纳百川\"}],\"notifyCount\":12,\"unreadCount\":11,\"country\":\"China\",\"access\":\"admin\",\"geographic\":{\"province\":{\"label\":\"浙江省\",\"key\":\"330000\"},\"city\":{\"label\":\"杭州市\",\"key\":\"330100\"}},\"address\":\"西湖区工专路 77 号\",\"phone\":\"0752-268888888\"}";

    CurrentUserDto currentUserDto = new CurrentUserDto();
    currentUserDto.setUserid("001");
    currentUserDto.setName("Serati Ma Angle");
    currentUserDto.setAccess("admin");
    currentUserDto.setEmail("antdesign@alipay.com");
    currentUserDto.setAvatar("https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png");
    currentUserDto.setUnreadCount(11);




    return currentUserDto;
  }

  @ApiOperation("获取用户通知")
  @GetMapping("/api/portal/notices")
  public Object getNotice(){
    String json = "{\"data\":[{\"id\":\"000000001\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"你收到了 14 份新周报\",\"datetime\":\"2017-08-09\",\"type\":\"notification\"},{\"id\":\"000000002\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png\",\"title\":\"你推荐的 曲妮妮 已通过第三轮面试\",\"datetime\":\"2017-08-08\",\"type\":\"notification\"},{\"id\":\"000000003\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png\",\"title\":\"这种模板可以区分多种通知类型\",\"datetime\":\"2017-08-07\",\"read\":true,\"type\":\"notification\"},{\"id\":\"000000004\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png\",\"title\":\"左侧图标用于区分不同的类型\",\"datetime\":\"2017-08-07\",\"type\":\"notification\"},{\"id\":\"000000005\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png\",\"title\":\"内容不要超过两行字，超出时自动截断\",\"datetime\":\"2017-08-07\",\"type\":\"notification\"},{\"id\":\"000000006\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg\",\"title\":\"曲丽丽 评论了你\",\"description\":\"描述信息描述信息描述信息\",\"datetime\":\"2017-08-07\",\"type\":\"message\",\"clickClose\":true},{\"id\":\"000000007\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg\",\"title\":\"朱偏右 回复了你\",\"description\":\"这种模板用于提醒谁与你发生了互动，左侧放『谁』的头像\",\"datetime\":\"2017-08-07\",\"type\":\"message\",\"clickClose\":true},{\"id\":\"000000008\",\"avatar\":\"https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg\",\"title\":\"标题\",\"description\":\"这种模板用于提醒谁与你发生了互动，左侧放『谁』的头像\",\"datetime\":\"2017-08-07\",\"type\":\"message\",\"clickClose\":true},{\"id\":\"000000009\",\"title\":\"任务名称\",\"description\":\"任务需要在 2017-01-12 20:00 前启动\",\"extra\":\"未开始\",\"status\":\"todo\",\"type\":\"event\"},{\"id\":\"000000010\",\"title\":\"第三方紧急代码变更\",\"description\":\"冠霖提交于 2017-01-06，需在 2017-01-07 前完成代码变更任务\",\"extra\":\"马上到期\",\"status\":\"urgent\",\"type\":\"event\"},{\"id\":\"000000011\",\"title\":\"信息安全考试\",\"description\":\"指派竹尔于 2017-01-09 前完成更新并发布\",\"extra\":\"已耗时 8 天\",\"status\":\"doing\",\"type\":\"event\"},{\"id\":\"000000012\",\"title\":\"ABCD 版本发布\",\"description\":\"冠霖提交于 2017-01-06，需在 2017-01-07 前完成代码变更任务\",\"extra\":\"进行中\",\"status\":\"processing\",\"type\":\"event\"}]}";

    return JSONUtil.parseObj(json);
  }

  @GetMapping("/api/portal/fetchMenuData")
  public Object fetchMenuData(){
    String json = "[\n"
        + "    {\n"
        + "      \"path\": \"/dashboard\",\n"
        + "      \"name\": \"dashboard\",\n"
        + "      \"icon\": \"dashboard\",\n"
        + "      \"children\": [\n"
        + "        {\n"
        + "          \"name\": \"analysis\",\n"
        + "          \"icon\": \"smile\",\n"
        + "          \"path\": \"/dashboard/analysis\",\n"
        + "        },\n"
        + "        {\n"
        + "          \"name\": \"monitor\",\n"
        + "          \"icon\": \"smile\",\n"
        + "          \"path\": \"/dashboard/monitor\",\n"
        + "        },\n"
        + "        {\n"
        + "          \"name\": \"workplace\",\n"
        + "          \"icon\": \"smile\",\n"
        + "          \"path\": \"/dashboard/workplace\",\n"
        + "        },\n"
        + "        {\n"
        + "          \"name\": \"demo\",\n"
        + "          \"icon\": \"smile\",\n"
        + "          \"path\": \"/dashboard/demo\",\n"
        + "        },\n"
        + "      ],\n"
        + "    },\n"
        + "  ]";


    return antdUserService.getMenuData(1L);
  }

}
